﻿using HarmonyLib;
using JSONClass;
using KBEngine;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using static UnityEngine.UI.Image;

namespace ncs.lingtian
{
    public partial class LingTianMod
    {
        private static int monthCostOriginValue = 0;
        private static int maxLingShiOriginValue = 0;
        /// <summary>
        ///  修正50年的最大灵力
        /// </summary>
        [HarmonyPostfix, HarmonyPatch(typeof(UILingLiChongNeng), "Init")]
        public static void UILingLiChongNeng_Init_Postfix(UILingLiChongNeng __instance)
        {
            Info($"UILingLiChongNeng_Init_Postfix");
            var traverse = Traverse.Create(__instance);
            var df = traverse.Field("df").GetValue<DongFuData>();
            //修正50年的最大灵力值
            monthCostOriginValue = traverse.Field("monthCost").GetValue<int>();
            var value = Helper.Caculate(DFZhenYanLevel.DataDict[df.JuLingZhenLevel].lingtiancuishengsudu, MainMod.pingzi.Value, 1);
            traverse.Field("monthCost").SetValue(value);

            maxLingShiOriginValue = traverse.Field("maxLingShi").GetValue<int>();
            var maxLinshi = Mathf.Min((int)Tools.instance.getPlayer().money, UIDongFu.Inst.LingTian.CuiShengLingShi50Year);
            traverse.Field("maxLingShi").SetValue(maxLinshi);

            traverse.Method("UpdateUI").GetValue();
            Info($"monthCostOriginValue:{monthCostOriginValue} maxLingShiOriginValue:{maxLingShiOriginValue} maxLinshi:{maxLinshi} monthCost:{value}");
        }

        /// <summary>
        ///  扣除灵石重新计算
        /// </summary>
        [HarmonyPostfix, HarmonyPatch(typeof(UILingLiChongNeng), "UpdateUI")]
        public static void UILingLiChongNeng_UpdateUI_Postfix(UILingLiChongNeng __instance)
        {
            Info($"UILingLiChongNeng_UpdateUI_Postfix");
            var traverse = Traverse.Create(__instance);
            var curLingShi = traverse.Field("curLingShi").GetValue<Text>();
            var baseLingShi = traverse.Field("baseLingShi").GetValue<int>();
            var addLingShi = traverse.Field("addLingShi").GetValue<int>();
            
            curLingShi.text = (baseLingShi + addLingShi*MainMod.pingzi.Value).ToString();  
        }


        /// <summary>
        ///  扣除灵石重新计算
        /// </summary>
        [HarmonyPrefix, HarmonyPatch(typeof(UILingLiChongNeng), nameof(UILingLiChongNeng.Ok))]
        public static bool UILingLiChongNeng_Ok_Prefix(UILingLiChongNeng __instance)
        {
            Info($"UILingLiChongNeng_Ok_Postfix");
            var traverse = Traverse.Create(__instance);
            var df = traverse.Field("df").GetValue<DongFuData>();
            var addLingShi = traverse.Field("addLingShi").GetValue<int>();

            df.CuiShengLingLi += addLingShi*MainMod.pingzi.Value;
            df.Save();
            UIDongFu.Inst.InitData();
            UIDongFu.Inst.LingTian.RefreshUI();
            Tools.instance.getPlayer().AddMoney(-addLingShi);
            __instance.Close();
            return false;
        }


        /// <summary>
        /// 催生剩余时间的计算修复
        /// </summary> 
        [HarmonyPrefix, HarmonyPatch(typeof(UILingTianPanel), nameof(UILingTianPanel.CalcSpeed))]
        public static bool UILingTianPanel_CalcSpeed_Prefix(UILingTianPanel __instance, int exLingShi = 0)
        {
            Info($"UILingTianPanel_CalcSpeed_Prefix exLingShi:{exLingShi}");

            KBEngine.Avatar player = PlayerEx.Player;
            string index = string.Format("DongFu{0}", DongFuManager.NowDongFuID);
            int num = 0;
            for (int i = 0; i < DongFuManager.LingTianCount; i++)
            {
                if (player.DongFuData[index]["LingTian"][string.Format("Slot{0}", i)]["ID"].I > 0)
                {
                    num++;
                }
            }
            int i2 = player.DongFuData[index]["LingYanLevel"].I;
            int i3 = player.DongFuData[index]["JuLingZhenLevel"].I;
            int lingtiansudu = DFLingYanLevel.DataDict[i2].lingtiansudu;
            int lingtiansudu2 = DFZhenYanLevel.DataDict[i3].lingtiansudu;
            int lingtiancuishengsudu = Helper.Caculate(DFZhenYanLevel.DataDict[i3].lingtiancuishengsudu, MainMod.pingzi.Value, 1);
            var cuiShengLingLi=Helper.Caculate(player.DongFuData[index]["LingTian"]["CuiShengLingLi"].I, MainMod.pingzi.Value, 1);
            Info($"cuiShengLingLi:{player.DongFuData[index]["LingTian"]["CuiShengLingLi"].I}->{cuiShengLingLi}");
            int num2 = cuiShengLingLi + exLingShi;
            Info($"num2:{cuiShengLingLi}->{num2}");
            if (num > 0)
            {
                __instance.BaseSpeedPer = (float)(lingtiansudu + lingtiansudu2) / (float)num;
                __instance.CuiShengSpeedPer = (float)DFZhenYanLevel.DataDict[i3].lingtiancuishengsudu / (float)num;

                Info($"BaseSpeedPer:{__instance.BaseSpeedPer} CuiShengSpeedPer:{__instance.CuiShengSpeedPer} lingtiansudu:{lingtiansudu} lingtiansudu2:{lingtiansudu2} lingtiancuishengsudu:{lingtiancuishengsudu}");

            }
            __instance.CuiShengTime = num2 / lingtiancuishengsudu;
            __instance.CuiShengLingShi50Year = 600 * lingtiancuishengsudu;

            return false;
        }
    }
}
